package com.yunhe.authority.repository.company;

import com.yunhe.authority.domain.company.Employee;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmployeeRepository extends Neo4jRepository<Employee, Long> {

    @Query("match (e:Employee) where e.phone={phone} return e order by e.createTime limit 1")
    Employee findByPhone(@Param("phone") String phone);

    @Query("match (e:Employee) where e.phone={phone} return e")
    List<Employee> findListByPhone(@Param("phone")String phone);

    /**
     * 通过ids删除员工
     * @param ids
     */
    @Query("match (e:Employee) where ID(e) IN {ids} delete e")
    void deleteByIds(@Param("ids") List<Long> ids);

    /**
     * 员工与企业进行绑定
     * @param employeeId
     * @param companyId
     */
    @Query("start employee=node({employeeId}),company=node({companyId}) create (employee)-[:HAS_COMPANY]->(company)")
    void bindCompany(@Param("employeeId") Long employeeId, @Param("companyId") Long companyId);

    @Query("MATCH(e:Employee),(r:Role) where ID(e)={employeeId} AND ID(r) IN {roleIds} CREATE (e)-[:HAS_ROLE]->(r)")
    void bindCompanyRole(@Param("employeeId") Long employeeId, @Param("companyId") Long companyId, @Param("roleIds") List<Long> roleIds);

    @Query("match (e:Employee)-[c:HAS_COMPANY]->(company:Company) where ID(e) IN {employeeIds} delete c")
    void unbindAllCompanies(List<Long> employeeIds);

    @Query("match (e:Employee)-[r:HAS_ROLE]->(role:Role) where ID(e) IN {employeeIds} delete r")
    void unbindAllRoles(@Param("employeeIds") List<Long> employeeIds);

    @Query("start n=node({id}) set n.password={password} return n")
    Employee updatePassword(@Param("id") Long id, @Param("password") String password);

    @Query("match (n:Employee) where n.phone={phone} set n.password={password}")
    void updateAllSamePhoneEmployeePassword(@Param("phone") String phone, @Param("password") String password);

    @Query("match (e:Employee) where e.phone={phone} set e.password={password} return e")
    void updatePasswordByPhone(@Param("phone") String phone, @Param("password") String password);

    /**
     * 禁用/恢复用户
     * @param employeeIds
     * @param activity
     * @param remark
     * @param format
     * @return
     */
    @Query("match (e:Employee) where ID(e) IN {ids} SET e.activity={activity},e.updateTime={updateTime},e.remark={remark} return e")
    List<Employee> updateActivity(@Param("ids") List<Long> employeeIds, @Param("activity") String activity, @Param("remark") String remark, @Param("updateTime") String format);

    @Query("MATCH(e:Employee)-[hr:HAS_ROLE]->(r:Role) where ID(e)={employeeId} AND ID(r)={roleId} delete hr")
    void employeeUnbindCompanyRole(@Param("employeeId") Long employeeId, @Param("roleId") Long roleId);

    Employee findByPhoneAndId(String phone, Long id);

    @Query("match (e:Employee) where e.phone={phone} and e.password={password} return e")
    List<Employee> getEmployeesByPhoneAndPassword(@Param("phone") String phone, @Param("password") String password);
}
